package com.huanxing.cloud.mall.admin.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.common.log.annotation.SysLog;
import com.huanxing.cloud.mall.admin.service.IGoodsSpuService;
import com.huanxing.cloud.mall.common.entity.GoodsSpu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 商品spu前端控制器
 *
 * @author lijx
 * @since 2022/2/22 14:33
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/goodsspu")
@Api(value = "goodsspu", tags = "商品spu")
public class GoodsSpuController {

  private final IGoodsSpuService goodsSpuService;

  @ApiOperation(value = "商品列表")
  @PreAuthorize("hasAuthority('mall:goodsspu:page')")
  @GetMapping("/page")
  public Result page(Page page, GoodsSpu goodsSpu) {
    return Result.success(goodsSpuService.adminPage(page, goodsSpu));
  }

  @ApiOperation(value = "通过id查询商品")
  @PreAuthorize("hasAuthority('mall:goodsspu:get')")
  @GetMapping("/{id}")
  public Result getById(@PathVariable String id) {
    return Result.success(goodsSpuService.getSpuById(id));
  }

  @ApiOperation(value = "商品新增")
  @SysLog("新增商品")
  @PreAuthorize("hasAuthority('mall:goodsspu:add')")
  @PostMapping
  public Result add(@RequestBody GoodsSpu goodsSpu) {
    return Result.success(goodsSpuService.saveGoods(goodsSpu));
  }

  @ApiOperation(value = "商品修改")
  @SysLog("修改商品")
  @PreAuthorize("hasAuthority('mall:goodsspu:edit')")
  @PutMapping
  public Result edit(@RequestBody GoodsSpu goodsSpu) {
    return Result.success(goodsSpuService.updateGoods(goodsSpu));
  }

  @ApiOperation(value = "商品删除")
  @SysLog("删除商品")
  @PreAuthorize("hasAuthority('mall:goodsspu:del')")
  @DeleteMapping("/{id}")
  public Result del(@PathVariable String id) {
    return Result.success(goodsSpuService.removeById(id));
  }

  @ApiOperation(value = "商城数量统计查询")
  @GetMapping("/statistics")
  public Result statistics() {
    long allCount = goodsSpuService.count(Wrappers.lambdaQuery());
    LocalDateTime localDateTime = LocalDateTime.now();
    long todayCount =
        goodsSpuService.count(
            Wrappers.<GoodsSpu>lambdaQuery()
                .ge(GoodsSpu::getCreateTime, LocalDateTimeUtil.beginOfDay(localDateTime))
                .le(GoodsSpu::getCreateTime, LocalDateTimeUtil.endOfDay(localDateTime)));
    Map<String, Object> rt = Maps.newHashMap();
    // 今日数量
    rt.put("todayCount", todayCount);
    // 全部数量
    rt.put("allCount", allCount);
    return Result.success(rt);
  }
}
